from sklearn import datasets
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import load_model
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint
from matplotlib import pyplot as plt

# train
data = datasets.load_iris() #花数据集
# (xtrian,ytrain),(xtest,ytest) = mnist.load_data()

x = data.data               #数据
y = data.target             #标签

ylabels = to_categorical(y, num_classes=4)      #标签分为4类
seed = 7
np.random.seed(seed)

def create_model(optimizer='rmsprop', init='glorot_uniform'):
    model = Sequential()
    model.add(Dropout(rate=0.2))
    model.add(Dense(units=4,activation='relu',input_dim=4,kernel_initializer=init))  #全连接层
    # model.add(Dropout(rate=0.2))                                #Dropout层
    model.add(Dense(units=6,activation='relu',kernel_initializer=init))
    model.add(Dense(units=4,activation='softmax',kernel_initializer=init))
    learningrate=0.1   #学习率
    moment=0.9          #动量
    decayrate=0.005    #学习率衰减率
    sgd = SGD(lr=learningrate,momentum=moment,decay=decayrate,nesterov=False)   #Dropout参数

    model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy']) #模型编译，loss计算选择为交叉熵
    return model

model = create_model()
# filepath = 'weights-improvement.h5'         #保存模型
# checkpoint = ModelCheckpoint(filepath=filepath,monitor='val_acc',save_best_only=True,mode='max')   #指针 保存最佳模型
# callback = [checkpoint]   #回调函数
# history = model.fit(x,ylabels,epochs=200,batch_size=10,callbacks=callback)   #带回调函数的训练
history = model.fit(x,ylabels,epochs=200,batch_size=10)     #不带回调函数的训练
score = model.evaluate(x,ylabels)  #评估模型准确率
print(score[1]*100)                 #输出准确率

print(history.history.keys())       #结果可视化
plt.plot(history.history['accuracy'])
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.title('model accuracy')
plt.show()

plt.plot(history.history['loss'])
plt.xlabel('epochs')
plt.ylabel('loss')
plt.title('model loss')
plt.show()

# test   'weights-improvement.h5'
# newmodel = load_model()
# newmodel.summary()
# newmodel.fit(x,ylabels,epochs=200,batch_size=10)
# score = newmodel.evaluate(x,ylabels)
# print(score[1]*100)
# go = 100
#
# # app
# x_predict = x[go,:]
# predict = newmodel.predict(x_predict.reshape(1,4))
# predict = np.argmax(predict)#取最大值的位置
# print(predict)
# print(y[go])